use sqlx::{Pool, Postgres, Error};
use crate::common::rdius::ChatRedis;
use crate::service::model::chat_model::{ChatUser, MessageList, Message, MessageNum};

pub async fn get_message_list_sql(pool: &Pool<Postgres>, id: i32) ->Result<Vec<MessageList>,Error> {
    let message_list=sqlx::query_as::<_,MessageList>("	select send_id,receive_id,num,last_message, last_time,
	nick as receive_nick,logo as receive_logo,
	nick as send_nick,logo as send_logo
	from message_list,chat_user where receive_id=id and send_id=$1;")
        .bind(id).fetch_all(pool).await?;

    Ok(message_list)
}
pub async fn get_message_list_item_sql(pool: &Pool<Postgres>, id: usize,receive_id:usize) ->Result<MessageList,Error> {
    let sql=format!("	select send_id,receive_id,num,last_message,last_time,
	nick as receive_nick,logo as receive_logo,
	nick as send_nick,logo as send_logo
	from message_list,chat_user where receive_id=id and send_id={0} and receive_id={1};",id,receive_id);

    let message_list=sqlx::query_as::<_,MessageList>(&sql).fetch_one(pool).await?;

    Ok(message_list)
}

pub async fn get_message_list_by_receive_sql(pool: &Pool<Postgres>, receive_id: i32) ->Result<Vec<MessageList>,Error> {
    let message_list=sqlx::query_as::<_,MessageList>("	select send_id as id,send_id,receive_id,num,last_message,last_time,
	nick as receive_nick,logo as receive_logo,
	nick as send_nick,logo as send_logo
	from message_list,chat_user where send_id=id and receive_id=$1;")
        .bind(receive_id).fetch_all(pool).await?;

    Ok(message_list)
}


pub async fn get_message(pool: &Pool<Postgres>, id: i32,receive_id:i32)->Result<Vec<Message>,Error>{
    let sql=format!("select * from message where (user_id={0} and receive_id={1}) or (user_id={1} and receive_id={0}) order by id desc limit 60;",id,receive_id);

    let message=sqlx::query_as::<_,Message>(&sql).fetch_all(pool).await?;
    Ok(message)

}
pub async fn get_message_more_sql(pool: &Pool<Postgres>, id: usize,receive_id:usize,last_id:&str)->Result<Vec<Message>,Error>{
    let sql=format!("select * from message where ((user_id={0} and receive_id={1}) or (user_id={1} and receive_id={0})) and id<{2} order by id desc  limit 60;",id,receive_id,last_id);

    let message=sqlx::query_as::<_,Message>(&sql).fetch_all(pool).await?;
    Ok(message)

}

pub async fn get_message_num(pool:&Pool<Postgres>,id:i32,receive_id:i32)->Result<i32,Error> {
    let sql=format!("select num from message_list where send_id={0} and receive_id={1};",id,receive_id);

    let num:MessageNum=sqlx::query_as::<_,MessageNum>(&sql).fetch_one(pool).await?;
    Ok(num.num)
}


pub async fn get_user_info_list(pool: &Pool<Postgres>,id:usize)->Result<Vec<ChatUser>,Error> {
    let sql=format!("select id,phone,nick,logo from chat_user where id != {};",id);

    let chat_user = sqlx::query_as::<_, ChatUser>(&sql).fetch_all(pool).await?;
    Ok(chat_user)
}
pub async fn get_user_info(pool: &Pool<Postgres>, id: i32)->Result<ChatUser,Error> {
    let sql=format!("select id,phone,nick,logo from chat_user where id={};",id);

    let chat_user = sqlx::query_as::<_, ChatUser>(&sql)
        .bind(id).fetch_one(pool).await?;
    Ok(chat_user)
}


pub async fn exesql(pool:&Pool<Postgres>,sql:&str)->Result<(),Error>{
    sqlx::query(sql).execute(pool).await.unwrap();
    Ok(())
}
